<template>
  <div>
  <!-- 展示页面 -->
    <div class="CouponManage-xian" >
    <!-- 头部搜索 -->
      <div class="CouponManage-top">
        <span> 优惠卷状态:</span>
        <el-select v-model="value" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <span>优惠卷名称:</span>
        <el-input v-model="input" placeholder="请输入内容"></el-input>
        <el-button type="primary" @click="sou" class="sssss">搜索</el-button>
      </div>

      <div class="zhong">
        <el-button type="primary" @click="$router.push({name:'coupon-edit'})">新建优惠卷</el-button>
      </div>
    <!-- 表格渲染内容 -->
      <div class="CouponManage-xia">
        <el-table :data="sum" stripe style="width: 100%">
          <el-table-column prop="name" label="优惠卷名称" width="180">

          </el-table-column>
          <el-table-column prop="receive_type" label="使用范围" width="180">
            <template slot-scope="scope">
              <div>
                {{ scope.row | fanwei }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="valid_day" label="有效期" width="180">
            <template slot-scope="scope">
                {{ scope.row.valid_day  |  formatDate  }}
            </template>
          </el-table-column>
          <el-table-column prop="type" label="领取/发放" width="100">
             <template slot-scope="scope">
              <div>
                {{ scope.row.valid_start  }} /{{scope.row.valid_type}}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="grant" label="状态" width="100">
             <template slot-scope="scope">
              <div>
                {{ scope.row.grant  |  zhuang   }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="receive_type" label="类型" width="100">
              <template slot-scope="scope">
              <div>
                {{ scope.row.receive_type  |  lei   }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="180">
            <template slot-scope="scope">
            <span @click="w(scope.row.id)">详情</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    <!-- 分页器 -->
      <div class="block">

        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[1, 2, 3, 4]"
          :page-size="limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="list.length"
        >
        </el-pagination>
      </div>
    </div>

  </div>
</template>
<script>
import { getCoupon } from '../../../api'
export default {
  data() {
    return {
      page: 1,
      limit: 3,
      xiang: -1,
      options: [
        {
          value: '选项1',
          label: '结束'
        },
        {
          value: '选项5',
          label: '进行中'
        }
      ],
      list: [],
      lists: [],
      value: '',
      input: ''

    }
  },
  mounted() {
    getCoupon().then(res => {
      console.log(res)
      this.list = res.data.list
      this.lists = res.data.list
    })
  },
  computed: {
    sum() {
      return this.list.slice(
        this.limit * (this.page - 1),
        this.page * this.limit
      )
    }
  },
  filters: {
    fanwei: function(www) {
      let q = ''
      let w = ''
      if (www.status == 1) {
        q = '班课'
      }
      if (www.scope_type == 1) {
        w = '全部课程'
      }
      return q + '/' + w
    },
    zhuang: function(qqq) {
      if (qqq == 1) {
        return qqq = '领取中'
      } else {
        return qqq = '已结束'
      }
    },
    lei: function(qqq) {
      if (qqq == 1) {
        return qqq = '领取'
      } else {
        return qqq = '捐赠'
      }
    }
  },

  methods: {
    sou() {
      this.list = this.lists.filter(i => {
        // 过滤includes
        return i.name.includes(this.input) // 包含
      })
    },

    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          (this.ruleForm.name = ''),
          (this.ruleForm.region = ''),
          (this.ruleForm.type = []),
          (this.ruleForm.resource = ''),
          (this.ruleForm.desc = '')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    w(id) {
      this.$router.push({ path: 'coupon-detail', query: { id } })
      console.log(id)
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.limit = val
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page = val
    }
  }
}
</script>
<style lang="scss" scope>
.CouponManage-top {
  display: flex;
  .zhong {
    margin-top: 20px;
    margin-bottom: 20px;
  }
  span {
    width: 200px;
    height: 40px;
    line-height: 40px;
  }
  .sssss {
    height: 40px !important;
    line-height: 40px !important;
  }
}
.CouponManage-xia {
  span {
    color: yellowgreen;
  }
}
</style>
